<template>
  <div class="image-loader-container">
    <img v-if="!everythingDone" :src="placeholder" class="placeholder" />
    <img
      @load="imageLoader"
      :src="src"
      class="origin"
      :style="{ opacity: originOpacity, transition: `${duration}ms` }"
    />
  </div>
</template>

<script>
export default {
  props: {
    // 原始图片
    src: {
      type: String,
      requires: true,
    },
    // 占位图片
    placeholder: {
      type: String,
      requires: true,
    },
    // 原始图片加载完成后，切换到原始图经过的毫秒数
    duration: {
      type: Number,
      default: 500,
    },
  },
  data() {
    return {
      originLoaded: false,
      everythingDone: false,
    };
  },
  computed: {
    originOpacity() {
      return this.originLoaded ? 1 : 0;
    },
  },
  methods: {
    imageLoader() {
      this.originLoaded = true;
      setTimeout(() => {
        this.everythingDone = true;
        this.$emit("load");
      }, this.duration);
    },
  },
};
</script>
    
<style scoped lang="less">
@import "~@/styles/mixin.less";
.image-loader-container {
  height: 100%;
  width: 100%;
  position: relative;
  overflow: hidden;
  img {
    .self-fill();
    object-fit: cover;
  }
  .placeholder {
    // 虚化
    filter: blur(2vw);
  }
}
</style>